<template>
    <div>
        <el-card>
            <el-table border :data="skuArr">
                <el-table-column label="序号" type="index" width="100"></el-table-column>
                <el-table-column label="名称" property="skuName"></el-table-column>

                <el-table-column label="描述" property="skuDesc"></el-table-column>

                <el-table-column label="图片">
                    <template #="{ row }">
                        <img :src="row.skuDefaultImg" class="w-24 h-24" alt="">
                    </template>
                </el-table-column>

                <el-table-column label="重量" property="weight"></el-table-column>
                <el-table-column label="价格" property="price"></el-table-column>
                <el-table-column label="操作">
                    <template #="{ row }">
                        <el-button :type="row.isSale == 0 ? 'primary' : 'info'" size="small" @click="isSale(row)"
                            :icon="row.isSale == 0 ? 'SortUp' : 'SortDown'"></el-button>
                        <el-button type="primary" size="small" @click="Edit" icon="Edit"></el-button>
                        <el-button type="primary" size="small" @click="findSku(row)" icon="InfoFilled"></el-button>
                        <el-popconfirm width="220" confirm-button-text="确定" cancel-button-text="取消" icon="Delete"
                            icon-color="#626AEF" :title="`确认要删除${row.skuName}吗?`" @confirm="remove(row.id)">
                            <template #reference>
                                <el-button type="primary" size="small" icon="Delete"></el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" class="my-3"
                :page-sizes="[10, 20, 30, 40]" :background="true" layout=" prev, pager, next, jumper ,->,sizes,total, "
                :total="total" @size-change="handle" @current-change="getHasSku" />
        </el-card>


        <!-- 抽屉组件 展示商品详情 -->
        <el-drawer v-model="drawer">
            <!-- 标题 -->
            <template #header>
                <h4>查看商品sku详情</h4>
            </template>
            <template #default>
                <el-row class="my-3">
                    <el-col :span="6">名称</el-col>
                    <el-col :span="18">{{ find.skuName }}</el-col>
                </el-row>
                <el-row class="my-3">
                    <el-col :span="6">描述</el-col>
                    <el-col :span="18">{{ find.skuDesc }}</el-col>
                </el-row>
                <el-row class="my-3">
                    <el-col :span="6">价格</el-col>
                    <el-col :span="18">{{ find.price }}</el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">平台属性</el-col>
                    <el-col :span="18">
                        <el-tag class="m-2" v-for="item in AttrValueList">{{ item.valueName }}</el-tag>
                    </el-col>
                </el-row>
                <el-row class="my-3">
                    <el-col :span="6">销售属性</el-col>
                    <el-col :span="18">
                        <el-tag class="m-2" v-for="item in SaleAttrValueList">{{ item.saleAttrValueName }}</el-tag>
                    </el-col>
                </el-row>
                <el-row class="my-3">
                    <el-col :span="6">商品图片</el-col>
                    <el-col :span="18">
                        <el-carousel :interval="4000" type="card" height="200px">
                            <el-carousel-item v-for="item in ImageList" :key="item.id">
                                <img :src="item.imgUrl" alt="" style="width: 100%; height: 100%;">
                            </el-carousel-item>
                        </el-carousel>
                    </el-col>
                </el-row>
            </template>
            <template #footer>
            </template>
        </el-drawer>

    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from "vue"
// 引入接口
import { reqSkuList, reqDeleteSku, reqCancelSale, reqOnSale ,reqSkuInfo} from "../../../api/product/sku/index"
//引入ts类型
import type { SkuResponseData, SkuData } from "@/api/product/sku/type"
//引入消息组件
import { ElMessage } from 'element-plus'
//分页器当前的页码
let pageNo = ref<any>(1)
//每一页展示的数据条数
let pageSize = ref<any>(10)

let total = ref<number>(0)

///SKU所有数据
let skuArr = ref<SkuData[]>([])

//控制抽屉的显示与隐藏
const drawer = ref<boolean>(false)

//详情数据
const find = ref<any>()

//详情平台属性数据
const  AttrValueList=ref<any>()

//详情销售属性数据
const  SaleAttrValueList=ref<any>()


//详情图片数据
const ImageList=ref<any>()

// 进入页面发送请求数据的钩子函数
onMounted(() => {
    getHasSku()
})
// 展示数据的函数
const getHasSku = async (pager = 1) => {
    pageNo.value = pager
    let result: SkuResponseData = await reqSkuList(pageNo.value, pageSize.value)
    if (result.code == 200) {
        total.value = result.data.total
        skuArr.value = result.data.records
    }
    // console.log(skuArr.value);
}
//分页器下拉菜单发生变化时回调
const handle = () => {
    getHasSku()
}
//上下架按钮的回调
const isSale = async (row: any) => {
    console.log(row);
    //如果当前的商品isSale为0,说明当前的商品是下架的状态 ->更新为上架架状态 
    if (row.isSale == 0) {
        let result: any = await reqOnSale(row.id)
        if (result.code == 200) {
            ElMessage({
                showClose: true,
                message: '上架成功',
                type: 'success',
            })
        }
        getHasSku()
    }
    //如果当前的商品isSale为1,说明当前的商品是上架的状态 ->更新为下架状态 
    if (row.isSale == 1) {
        let result: any = await reqCancelSale(row.id)
        if (result.code == 200) {
            ElMessage({
                showClose: true,
                message: '下架成功',
                type: 'info',
            })
        }
        getHasSku()
    }
}

//编辑按钮的回调
const Edit = () => {
    ElMessage({
        showClose: true,
        message: '正在努力加班加点开发中!......',
        type: 'success',
    })
}


//sku详情的回调
const findSku = async(row: any) => {
    drawer.value = true
    find.value = row
    // console.log(row);
    
    let result:any=await reqSkuInfo(row.id)
    if(result.code==200){
        AttrValueList.value=result.data.skuAttrValueList
        SaleAttrValueList.value=result.data.skuSaleAttrValueList
        ImageList.value=result.data.skuImageList
        // console.log(AttrValueList);
        // console.log(SaleAttrValueList);
        // console.log(ImageList);
        // console.log(result.data.skuAttrValueList);
        // console.log(result.data.skuImageList);
        // console.log(result.data.skuSaleAttrValueList);
    }
}

//删除按钮的回调
const remove = async (skuId: any) => {
    let result: any = await reqDeleteSku(skuId)
    if (result.code == 200) {
        ElMessage({
            showClose: true,
            message: '删除SKU成功',
            type: 'success',
        })
        getHasSku()

    } else {
        ElMessage({
            showClose: true,
            message: '删除SKU失败!',
            type: 'info',
        })
    }
}
</script>

<style lang="scss" scoped>
//指示器
.demonstration {
  color: var(--el-text-color-secondary);
}

.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>